﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace SmartBlast
{
    public partial class frmOrders : Form
    {
        private static string TABLE_NAME = "Orders";
        private static string[] HEADERS = new string[] { "Customer Id", "Product Model", "Date", "Quantity"};
        private static string[] FIELD_NAMES = new string[] { "Customer_Id", "Product_Model", "Date", "Quantity"};
        private string[] fields;
        private DataTable ordersDataTable;
        private DataBase_BUS databaseBUS;
        private Main main;

        public frmOrders(Main main)
        {
            this.main = main;
            InitializeComponent();
            databaseBUS = DataBase_BUS.getInstance();
           
        }

        private void btn_search_Click(object sender, EventArgs e)
        {
            collectFields();
            ordersDataTable = databaseBUS.selectRecordFromJoinedTables("Customers", frmProducts.TABLE_NAME, TABLE_NAME,
                "Id", frmProducts.FIELD_NAMES[0], FIELD_NAMES, fields);
            refresh();
        }


        private void btn_Clear_Click(object sender, EventArgs e)
        {
            clearTextBoxes();
        }

        private void btn_showAll_Click(object sender, EventArgs e)
        {
            showAll();
        }

        private void collectFields()
        {
            fields = new string[] { txtCustomerId.Text,"'"+txtProductModel.Text+"'",
                (dateOrderDate.Checked==true)? "'"+dateOrderDate.Value.ToString()+"'": "", txtQuantity.Text};
        }

        private Boolean noFieldsAreBlank()
        {
            return (txtQuantity.Text != "" && txtCustomerId.Text != "" && txtProductModel.Text != "" &&
                dateOrderDate.Checked == true);
        }

        private void clearTextBoxes()
        {
            txtQuantity.Text = txtCustomerId.Text = txtProductModel.Text = "";
            dateOrderDate.Checked = false;
        }

        private void showAll()
        {
            //ordersDataTable = databaseBUS.selectRecrordFromTable(TABLE_NAME, null, null);
            //string query = "select Products.* , Customers.* , Orders.* from Products, Customers, Orders where Orders.Customer_Id=Customers.Id AND Orders.Product_Model=Products.Model";
            //ordersDataTable = DataBase_DAO.getInstance().executeSelectQuery(query);
            ordersDataTable = databaseBUS.selectAllRecordsFromJoinedTables("Customers", frmProducts.TABLE_NAME, TABLE_NAME, "Id", frmProducts.FIELD_NAMES[0], FIELD_NAMES[0], FIELD_NAMES[1]);
            refresh();
        }


        private void refresh()
        {
            if (ordersDataTable.Rows.Count == 0)
            {
                gridOrders.Visible = false;
                lbl_NoResults.Visible = true;
            }
            else
            {
                lbl_NoResults.Visible = false;
                gridOrders.Visible = true;
                gridOrders.DataSource = ordersDataTable;

                for (int i = 0; i < gridOrders.ColumnCount; i++)
                    gridOrders.Columns[i].ReadOnly = true;
            }
        }

       

        private void txtId_TextChanged(object sender, EventArgs e)
        {
            int id;
            bool success = int.TryParse(txtQuantity.Text, out id);
            String prevText = txtQuantity.Text.Substring(0, Math.Max(0, txtQuantity.Text.Length - 1));
            if (!success)
            {
                txtQuantity.Text = prevText;
            }
            else
            {
                txtQuantity.Text = id.ToString();
            }
        }


        private void btn_exit_Click(object sender, EventArgs e)
        {
            
            this.Hide();
        }


        private void frmOrders_Load(object sender, EventArgs e)
        {
            dateOrderDate.Checked = false;
        }

        private void frmOrders_Load_1(object sender, EventArgs e)
        {
            this.CenterToScreen();
        }

        private void gridOrders_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }





    }
}
